package chapter07

import scala.collection.mutable.ArrayOps

object Test18_ComplexWordCount {
  def main(args: Array[String]): Unit = {
    val tupleList = List(
      ("hello", 1),
      ("hello world", 2),
      ("hello scala", 3),
      ("hello spark from scala", 1),
      ("hello flink from scala", 2)
    )

    // 1.思路一,直接展开为普通版本
    val newStringList: List[String] = tupleList.map(
      kv => {
        (kv._1.trim + " ") * kv._2
      }
    )
    println(newStringList)

    val result: List[(String, Int)] = newStringList.flatMap(_.split(" ")).groupBy(word => word).map(kv => (kv._1, kv._2.size)).toList.sortBy(_._2)(Ordering[Int].reverse).take(3)
    println(result)

    // 2.思路2 直接基于预统计的结果进行转换
    // 1.将字符串打散成单词,并结合对应的个数包装成二元组
    val preCountList: List[(String, Int)] = tupleList.flatMap(tuple => {
      val strings: Array[String] = tuple._1.split(" ")
      strings.map(word => (word, tuple._2))
    })

    // 2.对二元组按照单词进行分组
    val preCountMap: Map[String, List[(String, Int)]] = preCountList.groupBy(_._1)

    //3. 叠加每个单词预统计的个数值
    val countMap: Map[String, Int] = preCountMap.mapValues(
      tupleList => tupleList.map(_._2).sum
    )
    println(countMap)
    //4.转换成List.排序取前3
    val countList: List[(String, Int)] = countMap.toList.sortWith(_._2 > _._2).take(3)
    println(countList)
  }
}
